package com.example.chang.law_doc.ui;

import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chang.law_doc.MainActivity;
import com.example.chang.law_doc.R;
import com.example.chang.law_doc.utils.ImmersiveStatusbar;
import com.example.chang.law_doc.view.CustomDialog;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by chang on 2018/3/11.
 * 项目描述：
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    RelativeLayout RL_1;
    Button btn_login_pass;
    TextView tv_register;
    TextView tv_welcome;
    EditText et_login_account;
    EditText et_login_password;
    private CustomDialog dialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        initView();
    }

    private void initView() {
        AssetManager mgr=getAssets();//得到AssetManager
        Typeface tf=Typeface.createFromAsset(mgr,"STXINWEI.TTF");//根据路径得到Typeface

        RL_1= (RelativeLayout) findViewById(R.id.RL_1);
        RL_1.getBackground().setAlpha(100);
        btn_login_pass= (Button) findViewById(R.id.btn_login_pass);
        btn_login_pass.getBackground().setAlpha(70);
        ImmersiveStatusbar.getInstance().Immersive(getWindow(),getSupportActionBar());
        btn_login_pass.setOnClickListener(this);
        tv_register= (TextView) findViewById(R.id.tv_register);
        tv_register.setOnClickListener(this);
        tv_welcome= (TextView) findViewById(R.id.tv_welcome);
        tv_welcome.setTypeface(tf);
        et_login_account = (EditText) findViewById(R.id.et_login_account);
        et_login_password = (EditText) findViewById(R.id.et_login_password);
        dialog=new CustomDialog(this,100,100,R.layout.dialog_loding,R.style.Theme_dialog, Gravity.CENTER,R.style.pop_anim_style);
        //屏幕外点击无效
        dialog.setCancelable(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login_pass:
                String name = et_login_account.getText().toString();
                String password = et_login_password.getText().toString();
                if (!TextUtils.isEmpty(name) & !TextUtils.isEmpty(password)) {
                    dialog.show();
                    BmobUser user = new BmobUser();
                    user.setUsername(name);
                    user.setPassword(password);
                    user.login(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            dialog.dismiss();
                            if (e == null) {
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, "登录失败:账号或密码有误！" , Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    break;
                }
                    case R.id.tv_register:
                        startActivity(new Intent(this, RegisterActivity.class));
                        break;
                }
        }
    }

